package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shop/api/requset"
	"shop/internal/dao"
	"shop/internal/model"
)

// 添加
func AddCollect(c *gin.Context) {
	//接收参数
	var req requset.AddCollectReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	//业务逻辑
	result := dao.Create(&model.Collect{
		UserId:  c.GetUint("userId"),
		GoodsId: req.GoodsId,
	})
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "收藏失败",
			"data": false,
		})
		return
	}

	//返回参数
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "收藏成功",
		"data": true,
	})
	return
}

// 取消收藏
func CancelCollect(c *gin.Context) {
	//接收参数
	var req requset.CancelCollectReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	//业务逻辑
	result := dao.Delete(&model.Collect{Id: req.Id})
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "取消收藏失败",
			"data": false,
		})
		return
	}

	//返回参数
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "取消收藏成功",
		"data": true,
	})
	return
}

// 查询
func ListCollect(c *gin.Context) {
	//接收参数
	var req requset.ListCollectReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	//业务逻辑
	var collects []model.Collect
	result := dao.GetListByFields(&model.Collect{
		Id:     req.Id,
		UserId: c.GetUint("userId"),
	}, &collects)
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "查询失败",
			"data": false,
		})
		return
	}

	//返回参数
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "查询成功",
		"data": collects,
	})
	return
}
